var $ = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   * 
   */
  data: {
    //banner图片
    imgUrls: [],
    pageNum: 1,
    pageSize: 10,
    scrollTop: 0,              // 搜索组件根据页面滚动高度设置透明度
    identState: "未认证",
    menus: [
      { menuUrl: "", imgUrl: "/images/menu/error.png", desc: "故障报修" ,menuId:'error'},
      { menuUrl: "/pages/duty/index", imgUrl: "/images/menu/duty.png", desc: "值班信息", menuId:'duty' },
      { menuUrl: "/pages/work/index", imgUrl: "/images/menu/work.png", desc: "工单管理", menuId: 'work' },
      { menuUrl: "/pages/check/index", imgUrl: "/images/menu/check.png", desc: "巡检信息",menuId: 'check'},
      { menuUrl: "/pages/message/index", imgUrl: "/images/menu/msg.png", desc: "我的消息", menuId: 'message'}
    ],//
    count: {"unreadMsgCount":0,"undoCheckCount":0,"undoWorkCount":0},
    token:'',
    loadingHidden:true
  },


  templateSend(e) {
    var _this = this;
    //var openId = _this.data.openId;
    // 表单需设置report-submit="true"
    var formId = e.detail.formId;
    console.log('formId:' + formId);
    if (!formId || 'the formId is a mock one' === formId) {
      _this.setData({
        logMessage: '[fail]请使用真机调试，否则获取不到formId'
      });
      return;
    }

    // 发送随机模板消息
    wx.request({
      url: app.data.app_server + "/weixin/send/template_send",
      data: {
        token: app.data.token,
        // openId: openId,
        formId: formId,
        
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status === 0) {
          _this.setData({
            logMessage: '发送模板消息成功[' + new Date().getTime() + ']'
          });
        }
      },
      fail: function (err) {
        _this.setData({
          logMessage: '[fail]' + JSON.stringify(err)
        });
      }
    });
  },
  /**
   * 跳转
   */
  menuHandle: function (e) {
    if(app.data.token){
      var menuUrl = e.currentTarget.dataset.url;
      if (menuUrl == "") {
        wx.showToast({
          title: '敬请期待',
          icon: 'none'
        })
      } else {
        wx.navigateTo({
          url: menuUrl
        })
      }
    }else{
      wx.navigateTo({
        url: '/pages/my/bindTelephone/bindTelephone'
      })
    }
    
  },
  /**
   * 加载banner
   */
  loadBanners: function () {
    console.log("开始加载轮播图片。。。");
    var that = this;
    $.get({
      url: getApp().data.app_server + '/weixin/get-banner',
    }).then((res) => {
      let data = res.data.msg;
      let urlArr = [];
      for (let i = 0; i < data.length; i++) {
        let url = getApp().data.app_server + data[i].imageUrl;
        urlArr.push(url);
      }
      console.log(urlArr);
      that.setData({
        imgUrls: urlArr
      });
    }).catch((err) => {
      console.log(err);
    });
  },
  //未读信息条数
  getUnreadMsgCount(_this, token) {
    _this.setData({
      loadingHidden:false
    })
    wx.request({
      url: app.data.app_server + "/weixin/message/count",
      method: 'post',
      data: {
        token: token,
        status: 0//0未读 1已读 空值获取全部
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        app.data.unreadMsgCount = res.data
        _this.setData({
          ['count.unreadMsgCount']: app.data.unreadMsgCount
        })
        console.log("unreadMsgCount:" + app.data.unreadMsgCount)
      }
    })
  },
  //待处理工单条数
  getUndoWorkCount(_this, token) {
    wx.request({
      url: app.data.app_server + "/weixin/workorder/count",
      method: 'post',
      data: {
        token: token,
        status: 0//0未读 1已读 空值获取全部
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      },
      success: function (res) {
        app.data.undoWorkCount = res.data.msg
        console.log("getUndoWorkCount:" + app.data.undoWorkCount)
        _this.setData({
          ['count.undoWorkCount']: app.data.undoWorkCount,
          loadingHidden:true
        })
      }
    })
  },
  //待巡检条数
  getUndoCheckCount(_this, token) {
    var strDateTime = util.formatTimeStamp(new Date().getTime(), 'Y-M-D') + ' 00:00'
    var endDateTime = util.formatTimeStamp(new Date().getTime(), 'Y-M-D') + ' 23:59'
    wx.request({
      url: app.data.app_server + "/weixin/polling/get-polling-record-count",
      method: 'post',
      data: {
        token: token,
        isExecute: 0,//0为巡检 1已巡检
        startDate: strDateTime,
        endDate: endDateTime
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        app.data.undoCheckCount = res.data.msg;
        console.log("undoCheckCount:" + _this.data.undoCheckCount)
        _this.setData({
          ['count.undoCheckCount']: app.data.undoCheckCount
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('监听页面加载');
    var _this = this;
    var token = app.data.token;
    _this.loadBanners();
    if (token){
      _this.getUnreadMsgCount(_this, token);
      _this.getUndoCheckCount(_this, token);
      _this.getUndoWorkCount(_this, token);
    }
    _this.setData({
      token:token
    })
    //引导用户授权
    wx.getUserInfo({
      success: res => {
        console.log(res)
        app.globalData.userInfo = res.userInfo
      },
      fail: () => {//授权失败,跳转授权页面
        wx.navigateTo({
          url: "/pages/authorization/index"
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('监听页面初次渲染完成');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    console.log('监听页面显示');
    var token = app.data.token;
    console.log("token:"+token);
    if (token) {
      console.log('监听页面显示2');
      _this.getUnreadMsgCount(_this, token);
      _this.getUndoCheckCount(_this, token);
      _this.getUndoWorkCount(_this, token);
    }
    /*this.setData({
      ['count.undoCheckCount']: app.data.undoCheckCount,
      ['count.unreadMsgCount']: app.data.unreadMsgCount,
      ['count.undoWorkCount']: app.data.undoWorkCount
    })*/
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
  * 页面上拉触底事件
  */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 页面滚动
   */
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  }
})
